package com.spark.lola.configuration;

import com.spark.lola.feign.ConfFeignService;
import com.spark.lola.feign.GdsFeignService;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.feign.SupFeignService;
import com.spark.lola.feign.service.PortalServiceOpenApi;
import com.spark.lola.logistics.mapper.*;
import com.spark.lola.logistics.service.impl.LogisticsFeignServiceImpl;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.service.gds.GdsFeignServiceImpl;
import com.spark.lola.service.gds.ICategoryInfoService;
import com.spark.lola.service.gds.IProductInfoService;
import com.spark.lola.service.pur.IOrderInfoService;
import com.spark.lola.service.sup.impl.SupFeignServiceImpl;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.configuration.OssProperties;
import com.spark.lola.support.mvc.service.ExcelServiceImpl;
import com.spark.lola.support.wms.configuration.JPushProperties;
import com.spark.lola.support.wms.configuration.WmsProperties;
import com.spark.lola.support.wms.constant.WmsConstant;
import com.spark.lola.support.wms.job.WmsJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.PostConstruct;

/**
 * 当前项目 Bean注入
 *
 * @Author: DengYeJun
 * @Date: 2020/4/22 11:28
 */
@Configuration
@EnableConfigurationProperties({WmsProperties.class, JPushProperties.class})
public class BiConfiguration {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private WmsProperties wmsProperties;

    @Autowired
    public BiConfiguration(ConfFeignService confFeignService){
        logger.info("===载入WMS配置===START===");
        ResponseBean responseBean = confFeignService.wms();
        if(responseBean.getCode()==200){
            wmsProperties = responseBean.convert(WmsProperties.class);
        }
        logger.info("===载入WMS配置===END===");

    }

    @Bean
    public WmsProperties wmsProperties(){
        return this.wmsProperties;
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder(10);
    }

    /**
     * 启动任务
     */
    @PostConstruct
    public void init() {
        // TODO 启动任务定制
        logger.info("===载入配置===START===");
        logger.info(
                wmsProperties.getLogistics().getExpires()
                        .get(WmsConstant.LOGISTICS_EXPIRE_COMPULSORY).toString());
        logger.info("===载入配置===END===");

    }

    @Bean
    public LogisticsFeignService logisticsFeignService(LocationInfoMapper locationInfoMapper, CarSubscribeMapper carSubscribeMapper,
                                                       LogisticsOrderInfoMapper logisticsOrderInfoMapper, ExpensesInfoMapper expensesInfoMapper,
                                                       DriverInfoMapper driverInfoMapper, CarInfoMapper carInfoMapper) {
        return new LogisticsFeignServiceImpl(locationInfoMapper, carSubscribeMapper, logisticsOrderInfoMapper, expensesInfoMapper,
                driverInfoMapper, carInfoMapper);
    }

    @Bean
    public GdsFeignService gdsFeignService(ICategoryInfoService categoryInfoService, IProductInfoService productInfoService,
                                           LogisticsFeignService logisticsFeignService) {
        return new GdsFeignServiceImpl(categoryInfoService, productInfoService, logisticsFeignService);
    }

    @Bean
    public SupFeignService supFeignService(SupplierInfoMapper supplierInfoMapper, IOrderInfoService orderInfoService, WmsProperties wmsProperties) {
        return new SupFeignServiceImpl(supplierInfoMapper, orderInfoService, wmsProperties);
    }

    @Bean
    public WmsJob wmsJob(PortalServiceOpenApi portalServiceOpenApi) {
        return new WmsJob(portalServiceOpenApi);
    }


    /**
     * excel组件服务注入
     *
     * @return
     */
    @Bean
    public ExcelServiceImpl excelService(OssProperties ossProperties) {
        return new ExcelServiceImpl(ossProperties);
    }

}
